#include <queue>
#include <vector>
using namespace std;


class Solution
{
    int dx[4] = {-1, 1, 0, 0};
    int dy[4] = {0, 0, -1, 1};
public:
    vector<vector<int>> highestPeak(const vector<vector<int>>& isWater)
    {
        int m = isWater.size(), n = isWater[0].size();
        vector<vector<int>> high(m, vector<int>(n, -1));
        queue<pair<int, int>> q;
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(isWater[i][j] == 1)
                {
                    q.emplace(i, j);
                    high[i][j] = 0;
                }
            }
        }

        while(!q.empty())
        {
            auto [a,b] = q.front();
            q.pop();
            for(int k=0; k<4; k++)
            {
                int x = a+dx[k], y = b+dy[k];
                if(x>=0 && x<m && y>=0 && y<n && high[x][y]==-1)
                {
                    q.emplace(x, y);
                    high[x][y] = high[a][b] + 1;
                }
            }
        }
        return high;
    }
};
